//
//  ZYJAccountManager.m
//  TravelingByCar
//
//  Created by Wendale on 15-3-14.
//  Copyright (c) 2015年 ZYJ. All rights reserved.
//

#import "ICAccountManager.h"
#import "ICCompletePofileInfo.h"

@interface ICAccountManager ()
@property(nonatomic, copy)NSString *welcomeKey;
@end

@implementation ICAccountManager

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.welcomeKey = [[[NSBundle mainBundle] infoDictionary]objectForKey:@"CFBundleShortVersionString"];
        self.accountInfo = [[ICAccountInfo alloc] init];
        self.accountInfo.profileInfo = [[ICCompletePofileInfo alloc] init];
        self.messageUnreadInfo = [[ICMessageUnreadInfo alloc] init];
        self.hostItem = [[ICHostInfoItem alloc] init];
        [self loadAccountFromCache];
       // [self requestUserHost];
    }
    return self;
}

- (void)loadAccountFromCache {
    ICAccountInfo *tempInfo = [NSKeyedUnarchiver unarchiveObjectWithFile:[self pathOfCacheFile]];
    if (tempInfo != nil) {
        self.accountInfo = tempInfo;
    }
}

- (void)updateAccount:(ICAccountInfo *)userInfo{
    self.accountInfo = userInfo;
    [self persistenceAccountInfo];
    [[NSNotificationCenter defaultCenter] postNotificationName:IC_ACCOUNT_DID_UPDATE_NOTIFICATION
                                                        object:nil
                                                      userInfo:nil];
}

- (BOOL)isLogIn{
    return self.accountInfo.token.length > 0 ? YES : NO;
}
//登陆失效或者手动退出时调用
- (void)logout{
    self.accountInfo = [[ICAccountInfo alloc] init];
    self.accountInfo.profileInfo = [[ICCompletePofileInfo alloc] init];
    [self clearCache];
    [[NSNotificationCenter defaultCenter] postNotificationName:IC_ACCOUNT_DID_LOGOUT_NOTIFICATION
                                                        object:nil
                                                      userInfo:nil];
}

- (void)clearCache{
    [[NSFileManager defaultManager] removeItemAtPath:[APP_CONTEXT.accountManager pathOfCacheFile] error:nil];
}

- (BOOL)hasShowedWelcomeView {
    return [[NSUserDefaults standardUserDefaults] boolForKey:self.welcomeKey];
}

- (void)setWelcomeShowed {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:self.welcomeKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


- (NSString *)iPhoneNumber{
    NSString *lastStr = @"";
    if ([self isIPhone6p]) {
        lastStr = @"6p";
    }else if([self isIPhone6]){
        lastStr = @"6";
    }else if([self isIPhone5]){
        lastStr = @"5";
    }else if([self isIPhone4]){
        lastStr = @"4";
    }
    return lastStr;
}

- (BOOL)isIPhone4{
    return kScreenHeight == 480 ? YES : NO;
}

- (BOOL)isIPhone5{
    return kScreenHeight == 568 ? YES : NO;
}

- (BOOL)isIPhone6{
    return kScreenHeight == 667 ? YES : NO;
}

- (BOOL)isIPhone6p{
    return kScreenHeight >= 736 ? YES : NO;
}

- (BOOL)hasShowedHomeView {
    return [[NSUserDefaults standardUserDefaults] boolForKey:@"ICHOMEVIEWCONTROLLER"];
}

- (void)setHomeShowed {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ICHOMEVIEWCONTROLLER"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)hasShowedFindProjectView {
    return [[NSUserDefaults standardUserDefaults] boolForKey:@"ICFINDPROJECTVIEWCONTROLLER"];
}

- (void)setFindProjectShowed {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ICFINDPROJECTVIEWCONTROLLER"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)hasShowedFindInvestorView {
    return [[NSUserDefaults standardUserDefaults] boolForKey:@"ICFINDINVESTORVIEWCONTROLLER"];
}

- (void)setFindInvestorShowed {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ICFINDINVESTORVIEWCONTROLLER"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)persistenceAccountInfo {
    if (![NSKeyedArchiver archiveRootObject:self.accountInfo toFile:[self pathOfCacheFile]]) {
        NSLog(@"error, cache account info failed!");
    }
}

- (void)upLoadClientId{
    if (APP_CONTEXT.accountManager.isLogIn && APP_CONTEXT.clientId.length > 0 ) {
//        [APP_CONTEXT.net pushGetuiClientID:APP_CONTEXT.clientId userId:APP_CONTEXT.accountManager.accountInfo.userId OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
//            if (code == 1) {
//                NSLog(@"message %@",message);
//            }
//            
//        }];
    }
}

- (void)refreshUnreadMessageCount{
//    [APP_CONTEXT.net getUnreadDynamicCountOnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
//        if (code == 1) {
//            APP_CONTEXT.accountManager.messageUnreadInfo.unReadNum = [info valueForKey:@"newNum"];
//            APP_CONTEXT.accountManager.messageUnreadInfo.totalNum = [info valueForKey:@"totalNum"];
//            [[NSNotificationCenter defaultCenter] postNotificationName:IC_MESSAGEUNREADCOUNT_UPDATE_NOTIFICATION
//                                                                object:nil
//                                                              userInfo:nil];
//        }
//    }];
}

- (NSString *)pathOfCacheFile {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryPath = [paths objectAtIndex:0];
    return [libraryPath stringByAppendingPathComponent:@"account_info.plist"];
}

- (void)requestUserHost{
    __weak typeof(self) weakSelf = self;
//    [APP_CONTEXT.net getUserInforOnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
//        if (code == 1) {
//            if ([info objectForKey:@"my"]) {
//                NSDictionary *dic = [info objectForKey:@"my"];
//                ICHostInfoItem *item = [ICHostInfoItem yy_modelWithDictionary:dic];
//                weakSelf.hostItem = item;
//            }
//        }
//    }];
//    
//    [APP_CONTEXT.net getMyProfileInforToken:APP_CONTEXT.accountManager.accountInfo.token OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
//        
//        if (code == 1) {
//            NSDictionary *profile = [info objectForKey:@"userInfo"];
//            ICCompletePofileInfo *profileInfo = [ICCompletePofileInfo yy_modelWithDictionary:profile];
//            APP_CONTEXT.accountManager.accountInfo.profileInfo = profileInfo;
//        }
//        
//    }];

}

- (void)refreshAccountInfo {
    __weak typeof (self) weakSelf = self;
   
//   [APP_CONTEXT.net getMyInfoToken:self.accountInfo.token OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
//       
//   }];
}

@end
